<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资源名称" prop="resourceName">
        <el-input
          v-model="queryParams.resourceName"
          placeholder="请输入资源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="年级类型" prop="gradeType">
        <el-select v-model="queryParams.gradeType" placeholder="请选择年级类型" clearable>
          <el-option
            v-for="dict in dict.type.grade_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科类型" prop="subjectType">
        <el-select v-model="queryParams.subjectType" placeholder="请选择学科类型" clearable>
          <el-option
            v-for="dict in dict.type.subject_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['classai:resource:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['classai:resource:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['classai:resource:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['classai:resource:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资源名称" align="center" prop="resourceName" />

      <el-table-column label="资源图片" align="center" prop="resourceImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.resourceImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="浏览数量" align="center" prop="browseNum" />-->
      <el-table-column label="年级类型" align="center" prop="gradeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.grade_type" :value="scope.row.gradeType"/>
        </template>
      </el-table-column>
      <el-table-column label="学科类型" align="center" prop="subjectType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.subject_type" :value="scope.row.subjectType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
            v-hasPermi="['classai:resource:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['classai:resource:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['classai:resource:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资源库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div  v-if="title != '详情'">
          <el-row>
            <el-col :span="12">
              <el-form-item label="资源名称" prop="resourceName">
                <el-input v-model="form.resourceName" placeholder="请输入资源名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年级类型" prop="gradeType">
                <el-select style="width: 100%" v-model="form.gradeType" placeholder="请选择年级类型">
                  <el-option
                    v-for="dict in dict.type.grade_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>

            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="学科类型" prop="subjectType">
                <el-select style="width: 100%" v-model="form.subjectType" placeholder="请选择学科类型">
                  <el-option
                    v-for="dict in dict.type.subject_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!--          <el-col :span="12">-->
            <!--            <el-form-item label="浏览数量" prop="browseNum">-->
            <!--              <el-input v-model="form.browseNum" type="number"  placeholder="请输入浏览数量" />-->
            <!--            </el-form-item>-->
            <!--          </el-col>-->
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="封面图片" prop="resourceImg">
                <image-upload v-model="form.resourceImg" :limit="1"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="资源备注" prop="remarks">
                <el-input v-model="form.remarks" :rows="6" type="textarea" placeholder="请输入资源备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row v-if="title === '详情'">
          <el-col :span="24">
            <el-form-item label="文件列表" prop="fileList">
              <el-button type="primary" @click="openFile"><i class="el-icon-plus"></i>选取文件</el-button>
<!--              <file-upload v-modal="form.resourceFiles"/>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="title === '详情'">
          <el-col :span="24">
            <el-table v-loading="loading" :data="form.resourceFiles">
<!--              <el-table-column type="index" width="50"></el-table-column>-->
              <el-table-column label="资源名称" align="center" prop="resourceName" />
              <el-table-column label="资源详情" align="center" prop="resourceDetails" />
              <el-table-column label="文件" align="center" show-overflow-tooltip prop="subjectType">
                <template slot-scope="scope">
                  <div>{{scope.row.resourceUrl}}</div>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-view"
                    @click="handleView(scope.row)"
                    v-hasPermi="['classai:resource:edit']"
                  >
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete1(scope.row)"
                    v-hasPermi="['classai:resource:remove']"
                  >
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="文件上传" :visible.sync="open1" width="60%" append-to-body>
      <el-form ref="form1" :model="resourceFiles" :rules="rules1" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源名称" prop="resourceName">
              <el-input v-model="resourceFiles.resourceName" placeholder="请输入资源名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源详情" >
              <el-input v-model="resourceFiles.resourceDetails" placeholder="请输入资源详情" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件上传" prop="resourceUrl">
              <file-upload :fileSize="50" v-model="resourceFiles.resourceUrl" :file-type="['mp4', 'wmv', 'avi', 'mpeg', 'png', 'jpg', 'jpeg', 'mp3']"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResource, getResource, delResource, addResource, updateResource } from "@/api/classai/resource";

export default {
  name: "Resource",
  dicts: ['grade_type', 'subject_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源库表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        resourceName: null,
        remarks: null,
        resourceImg: null,
        browseNum: null,
        gradeType: null,
        subjectType: null
      },
      resourceFiles:{},
      // 表单参数
      form: {
        resourceFiles: [
          {
            resourceUrl:'',
            resourceName:'',
            resourceDetails:'',
          }
        ],
      },
      // 表单校验
      rules: {
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
      },
      // 表单校验
      rules1: {
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        resourceUrl: [
          { required: true, message: "资源地址不能为空", trigger: "change" }
        ],
      }

    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开选取文件弹窗
    openFile(){
      this.open1 = true
    },
    /** 查询资源库列表 */
    getList() {
      this.loading = true;
      listResource(this.queryParams).then(response => {
        this.resourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    reset1(){
      this.resourceFiles = {
        resourceUrl:'',
        resourceName:'',
        resourceDetails:'',
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resourceName: null,
        remarks: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        resourceImg: null,
        resourceFiles: null,
        browseNum: null,
        status: null,
        gradeType: null,
        subjectType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源库";
    },
    handleDetails(row){
      this.reset();
      const ids = row.id || this.ids
      // getResource(id).then(response => {
      //   this.form = response.data;
      //   this.form.resourceFiles = response.data.resourceDetails
      //   this.open = true;
      //   this.title = "详情";
      // });
      // const routeData = this.$router.resolve({
      //   path: '/resourceinformation',
      //   query: { id: ids } // 可选
      // })
      // window.open(routeData.href, '_blank') // _blank 表示新标签页
      this.$router.push({ path: '/resource/resourceinformation', query: { id: ids } })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResource(id).then(response => {
        this.form = response.data;
        this.form.resourceFiles = response.data.resourceDetails
        this.open = true;
        this.title = "修改资源库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 资源地址提交按钮 */
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (!this.form.resourceFiles) {
            this.form.resourceFiles = [this.resourceFiles]
          } else {
            this.form.resourceFiles.push(this.resourceFiles)
          }
          this.open1 = false
          this.reset1()
        }
      });
    },
    handleView(e) {
      window.open(e.resourceUrl, '_blank')
    },
    handleDelete1(e){
      const index = this.form.resourceFiles.findIndex(it => it === e)
      if (index < 0) { return }
      this.form.resourceFiles.splice(index, 1)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资源库编号为"' + ids + '"的数据项？').then(function() {
        return delResource(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classai/resource/export', {
        ...this.queryParams
      }, `resource_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
